﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using KC.ExtensionMethods;

namespace KC.Tests
{
	[TestClass()]
	public class StringsTest
	{
		public TestContext TestContext { set; get; }

		[TestMethod()]
		[DeploymentItem("KC.ExtensionMethods.dll")]
		public void CountOfCharTest()
		{
			Assert.AreEqual(1,"a".CountOf('a'));
			Assert.AreEqual(1,"abc".CountOf('a'));
			Assert.AreEqual(1, "bad".CountOf('a'));
			Assert.AreEqual(1, "cba".CountOf('a'));
			Assert.AreEqual(2, "aa".CountOf('a'));
			Assert.AreEqual(2, "aabc".CountOf('a'));
			Assert.AreEqual(2, "baad".CountOf('a'));
			Assert.AreEqual(10, "aaaaaaaaaa".CountOf('a'));
			Assert.AreEqual(10, "aaaaaaaaaabc".CountOf('a'));
			Assert.AreEqual(10, "cbaaaaaaaaaa".CountOf('a'));
			Assert.AreEqual(10, "baaaaaaaaaad".CountOf('a'));
			Assert.AreEqual(0, "smoldering".CountOf('a'));
			Assert.AreEqual(0, "".CountOf('a'));
		}

		[TestMethod()]
		[DeploymentItem("KC.ExtensionMethods.dll")]
		public void CountOfStringTest()
		{
			Assert.AreEqual(1, "a".CountOf("a"));
			Assert.AreEqual(1, "abc".CountOf("a"));
			Assert.AreEqual(1, "bad".CountOf("a"));
			Assert.AreEqual(1, "cba".CountOf("a"));
			Assert.AreEqual(2, "aa".CountOf("a"));
			Assert.AreEqual(2, "aabc".CountOf("a"));
			Assert.AreEqual(2, "baad".CountOf("a"));
			Assert.AreEqual(10, "aaaaaaaaaa".CountOf("a"));
			Assert.AreEqual(10, "aaaaaaaaaabc".CountOf("a"));
			Assert.AreEqual(10, "cbaaaaaaaaaa".CountOf("a"));
			Assert.AreEqual(10, "baaaaaaaaaad".CountOf("a"));
			Assert.AreEqual(0, "smoldering".CountOf("a"));
			Assert.AreEqual(0, "".CountOf("a"));
			Assert.AreEqual(0, "smoldering".CountOf(""));
		}

		[TestMethod()]
		[DeploymentItem("KC.ExtensionMethods.dll")]
		public void GetMaxLineLengthTest()
		{
			Assert.AreEqual(5, Strings_Accessor.GetMaxLineLength("abcde"));
			Assert.AreEqual(4, Strings_Accessor.GetMaxLineLength("abcd\r\nabc\r\nab\r\na\r\n\rn"));
			Assert.AreEqual(0, Strings_Accessor.GetMaxLineLength(""));
		}

		[TestMethod()]
		[DeploymentItem("KC.ExtensionMethods.dll")]
		public void TruncateIfLongerTest()
		{
			string input = "abcdefghijklmnopqrstuvwxyz";
			Assert.AreEqual(input, input.TruncateIfLonger(26));
			Assert.AreEqual(input, input.TruncateIfLonger(27));
			Assert.AreEqual(input.Substring(0, 25), input.TruncateIfLonger(25));
			Assert.AreEqual("", "".TruncateIfLonger(int.MaxValue));
		}

		[TestMethod()]
		[DeploymentItem("KC.ExtensionMethods.dll")]
		public void WrapAtTest()
		{
			string input = "1234567890";
			Assert.AreEqual("12345\r\n67890", input.WrapAt(5));
			Assert.AreEqual(input, input.WrapAt(11));
		}

		[TestMethod]
		[ExpectedException(typeof(ArgumentNullException), "Cannot wrap a null string")]
		[DeploymentItem("KC.ExtensionMethods.dll")]
		public void WrapAtNullTest()
		{
			string n = null;
			n.WrapAt(5);
		}

		#region IndexesOf Tests

		[TestMethod]
		public void IndexesOfStringTest()
		{
			string input = "abcdabcdabcde";
			int[] expected = { 0, 4, 8 };
			int[] result = input.IndexesOf("abc");
			Assert.AreEqual(expected.Length, result.Length);
			for (int i=0; i < expected.Length; i++)
				Assert.AreEqual(expected[i], result[i]);
		}
		[TestMethod]
		public void IndexesOfStringEmptyTest() {
			string input = "abcdabcdabcde";
			Assert.AreEqual(0, input.IndexesOf("qwerty").Length);
		}
		[TestMethod]
		[ExpectedException(typeof(ArgumentNullException))]
		public void IndexesOfStringNullTest()
		{
			string input = null;
			input.IndexesOf("abc");
		}
		[TestMethod]
		public void IndexesOfCharTest() 
		{
			string input = "abcdabcdabcde";
			int[] expected = { 0, 4, 8};
			int[] result = input.IndexesOf('a');
			Assert.AreEqual(expected.Length, result.Length);
			for (int i = 0; i < expected.Length; i++)
				Assert.AreEqual(expected[i], result[i]);
		}
		[TestMethod]
		public void IndexesOfCharEmptyTest()
		{
			string input = "abcdabcdabcde";
			int[] result = input.IndexesOf('z');
			Assert.AreEqual(0, result.Length);
		}
		[TestMethod]
		[ExpectedException(typeof(ArgumentNullException))]
		public void IndexOfCharNullTest()
		{
			string input = null;
			input.IndexesOf('z');
		}

		#endregion IndexesOf Tests
	}
}
